package com.tengfei.api.question;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tengfei.common.annotation.Log;
import com.tengfei.common.annotation.RepeatSubmit;
import com.tengfei.common.core.controller.BaseController;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.core.validate.AddGroup;
import com.tengfei.common.core.validate.EditGroup;
import com.tengfei.common.enums.BusinessType;
import com.tengfei.common.utils.poi.ExcelUtil;
import com.tengfei.main.question.domain.bo.QbQuBo;
import com.tengfei.main.question.domain.bo.QbQuQueryBo;
import com.tengfei.main.question.domain.bo.QbUserBo;
import com.tengfei.main.question.domain.bo.QuestionBankBo;
import com.tengfei.main.question.domain.vo.QbUserVo;
import com.tengfei.main.question.domain.vo.QuestionBankVo;
import com.tengfei.main.question.service.IQuestionBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 题库Controller
 *
 * @author tengfei
 * @date 2022-07-28
 */
@Validated
@Api(value = "题库控制器", tags = {"题库管理"})
@RestController
@RequestMapping("/question/questionBank")
public class QuestionBankController extends BaseController {

    @Resource
    private IQuestionBankService iQuestionBankService;

    /**
     * 题库批量绑定题目
     */
    @ApiOperation("题库批量绑定题目")
    @SaCheckPermission("question:questionBank:add")
    @Log(title = "题库批量绑定题目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/qu")
    public R<Void> addQu(@RequestBody List<QbQuBo> bos) {
        return toAjax(iQuestionBankService.insertBatchQuByBo(bos) ? 1 : 0);
    }

    /**
     * 题库批量解绑题目
     */
    @ApiOperation("题库批量解绑题目")
    @SaCheckPermission("question:questionBank:remove")
    @Log(title = "题库批量解绑题目", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @DeleteMapping("/qu")
    public R<Void> delQu(@RequestBody List<QbQuBo> bos) {
        return toAjax(iQuestionBankService.deleteBatchQuById(bos) ? 1 : 0);
    }


    /**
     * 查询题库列表
     */
    @ApiOperation("查询题库列表")
    @SaCheckPermission("question:questionBank:list")
    @GetMapping("/list")
    public TableDataInfo<QuestionBankVo> list(QuestionBankBo bo, PageQuery pageQuery) {
        return iQuestionBankService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出题库列表
     */
    @ApiOperation("导出题库列表")
    @SaCheckPermission("question:questionBank:export")
    @Log(title = "导出题库列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(QuestionBankBo bo, HttpServletResponse response) {
        List<QuestionBankVo> list = iQuestionBankService.queryList(bo);
        ExcelUtil.exportExcel(list, "题库", QuestionBankVo.class, response);
    }

    /**
     * 获取题库详细信息
     */
    @ApiOperation("获取题库详细信息")
    @SaCheckPermission("question:questionBank:query")
    @GetMapping("/{qbId}")
    public R<QuestionBankVo> getInfo(@ApiParam("题库id")
                                     @NotNull(message = "题库id不能为空")
                                     @PathVariable("qbId") Long qbId) {
        return R.ok(iQuestionBankService.queryById(qbId));
    }

    /**
     * 新增题库
     */
    @ApiOperation("新增题库")
    @SaCheckPermission("question:questionBank:add")
    @Log(title = "新增题库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody QuestionBankBo bo) {
        return toAjax(iQuestionBankService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改题库
     */
    @ApiOperation("修改题库")
    @SaCheckPermission("question:questionBank:edit")
    @Log(title = "修改题库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QuestionBankBo bo) {
        return toAjax(iQuestionBankService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 批量删除题库
     */
    @ApiOperation("批量删除题库")
    @SaCheckPermission("question:questionBank:remove")
    @Log(title = "批量删除题库", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    public R<Void> remove(@ApiParam("题库id数组")
                          @NotEmpty(message = "题库id数组不能为空")
                          @RequestParam("qbIds") List<Long> ids) {
        return toAjax(iQuestionBankService.deleteWithValidByIds(ids) ? 1 : 0);
    }


    /**
     * 查询题库已绑定题目列表
     */
    @ApiOperation("查询题库已绑定题目列表")
    @SaCheckPermission("question:questionBank:list")
    @GetMapping("/list/qu/bind")
    public TableDataInfo bindQuList(QbQuQueryBo bo, PageQuery pageQuery) {
        return iQuestionBankService.bindQuList(bo, pageQuery);
    }

    /**
     * 查询题库未绑定题目列表
     */
    @ApiOperation("查询题库未绑定题目列表")
    @SaCheckPermission("question:questionBank:list")
    @GetMapping("/list/qu/unBind")
    public TableDataInfo unBindQuList(QbQuQueryBo bo, PageQuery pageQuery) {
        return iQuestionBankService.unBindQuList(bo, pageQuery);
    }


    /**
     * 查询题库已绑定用户列表
     */
    @ApiOperation("查询题库已绑定用户列表")
    @SaCheckPermission("question:questionBank:list")
    @GetMapping("/list/user/bind")
    public TableDataInfo<QbUserVo> bindUserList(QbUserBo bo, PageQuery pageQuery) {
        return iQuestionBankService.bindUserList(bo, pageQuery);
    }

    /**
     * 查询题库未绑定用户列表
     */
    @ApiOperation("查询题库未绑定用户列表")
    @SaCheckPermission("question:questionBank:list")
    @GetMapping("/list/user/unBind")
    public TableDataInfo<QbUserVo> unBindUserList(QbUserBo bo, PageQuery pageQuery) {
        return iQuestionBankService.unBindUserList(bo, pageQuery);
    }

    /**
     * 题库批量绑定用户
     */
    @ApiOperation("题库批量绑定用户")
    @SaCheckPermission("question:questionBank:add")
    @Log(title = "题库批量绑定用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/user")
    public R<Void> addUser(@RequestBody List<QbUserBo> bos) {
        return toAjax(iQuestionBankService.insertBatchUserByBo(bos) ? 1 : 0);
    }

    /**
     * 题库批量解绑用户
     */
    @ApiOperation("题库批量解绑用户")
    @SaCheckPermission("question:questionBank:remove")
    @Log(title = "题库批量解绑用户", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @DeleteMapping("/user")
    public R<Void> delUser(@RequestBody List<QbUserBo> bos) {
        return toAjax(iQuestionBankService.deleteBatchUserById(bos) ? 1 : 0);
    }


}
